﻿using DevelopTool.Bll.DbBlls;
using DevelopTool.Common;
using DevelopTool.Forms.BaseForms;
using DevelopTool.Forms.CommonControls;
using DevelopTool.Forms.DbForms.FunctionForms;
using DevelopTool.Forms.DbForms.JobForms;
using DevelopTool.Forms.DbForms.PackageForms;
using DevelopTool.Forms.DbForms.ProcedureForms;
using DevelopTool.Forms.DbForms.ReferenceForms;
using DevelopTool.Forms.DbForms.TriggerForms;
using DevelopTool.Forms.DbForms.ViewForms;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model;
using DevelopTool.Model.CommonModels;
using DevelopTool.Model.DbModels;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace DevelopTool.Forms.DbForms.SequenceForms
{
    public partial class OracleSequenceForm : BaseUIPage
    {
        /// <summary>
        /// 需要固定的首行列名
        /// </summary>
        private string _fixedFirstRowColumnName = string.Empty;
        /// <summary>
        /// 需要固定的首行列名
        /// </summary>
        public string FixedFirstRowColumnName
        {
            get
            {
                return this._fixedFirstRowColumnName;
            }
            set
            {
                this._fixedFirstRowColumnName = value;
            }
        }

        /// <summary>
        /// 连接信息
        /// </summary>
        private DbInfo dbInfo;

        /// <summary>
        /// Oracle操作类
        /// </summary>
        private OracleDbBll oracleDbBll;

        /// <summary>
        /// 查询条件
        /// </summary>
        private string queryKeyword;

        /// <summary>
        /// 查询实例
        /// </summary>
        private string queryDataBase;

        /// <summary>
        /// 查询资源名
        /// </summary>
        private string queryInfoName;

        /// <summary>
        /// DataGridView列信息集合
        /// </summary>
        private List<DataGridColumn> dataGridColumns = new List<DataGridColumn>()
              {
                { new DataGridColumn("SEQUENCE_NAME"){ColumnText="序列名",Width=0,ColumnType = ColumnType.DefaultFilter | ColumnType.RowMain}},
                { new DataGridColumn("TEXT"){ColumnText="序列Sql",Width=-1,ColumnType =ColumnType.DefaultFilter|ColumnType.CanClick|ColumnType.LongText,ShowFormProperty=new ShowFormProperty(){
                BaseNameColumn="SEQUENCE_NAME"
                } }},
                { new DataGridColumn("OWNER"){ColumnText="实例",Width=100}},
                { new DataGridColumn("REFCOUNT"){ColumnText="依赖数量",Width=-1, ColumnType = ColumnType.CanClick}},
                { new DataGridColumn("MIN_VALUE"){ColumnText="最小值",Width=0}},
                { new DataGridColumn("MAX_VALUE"){ColumnText="最大值",Width=0}},
                { new DataGridColumn("INCREMENT_BY"){ColumnText="递增值",Width=0}},
                { new DataGridColumn("LAST_NUMBER"){ColumnText="当前值",Width=0}},
              };


        public OracleSequenceForm(DbInfo dbInfo)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
        }

        public OracleSequenceForm(DbInfo dbInfo,string queryKeyword)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
            this.queryKeyword = queryKeyword;
        }

        public OracleSequenceForm(DbInfo dbInfo,string queryDataBase,string queryInfoName)
        {
            InitializeComponent();

            this.dbInfo = dbInfo.CloneObj();
            this.queryDataBase = queryDataBase;
            this.queryInfoName = queryInfoName;
        }

        private void OracleSynonymForm_Load(object sender,EventArgs e)
        {
            this.oracleDbBll = new OracleDbBll(dbInfo);

            this.dataDisplayControl.DbInfo = this.dbInfo;

            this.dataDisplayControl.BindContextMenuStrip(this.ContextMenuStripDgv);//绑定右键菜单

            InitDate();

            if(!string.IsNullOrEmpty(this._fixedFirstRowColumnName))
            {
                this.dataDisplayControl.ShowTopSearhTextBox = false;
                this.dataDisplayControl.FixedFirstRowText(this._fixedFirstRowColumnName);
            }
        }


        private void InitDate()
        {
            DataTable dataTable = new DataTable();
            LoadingHelper.ShowLoading("获取序列数据",() =>
            {
                dataTable = oracleDbBll.GetSequences(this.queryDataBase,this.queryInfoName);
            });

            this.dataDisplayControl.BindData(dataTable,this.dataGridColumns,"SEQUENCE_NAME ASC");

            if(!string.IsNullOrEmpty(queryKeyword)) this.dataDisplayControl.Search(queryKeyword);

        }

        private void dataDisplayControl_DataGridViewCellClick(DataGridViewEventArgs dataGridViewEventArgs)
        {
            if(dataGridViewEventArgs.MouseButton == MouseButtons.Left)//判断点的是不是左键
            {
                if(dataGridViewEventArgs.Column.Name == "TEXT" && string.IsNullOrEmpty(dataGridViewEventArgs.Cell.Value.ToString()))
                {
                    string tableName = dataGridViewEventArgs.Row.Cells["SEQUENCE_NAME"].Value.ToString();//取得表名
                    string owner = dataGridViewEventArgs.Row.Cells["OWNER"].Value.ToString();//取得表所有者
                    string ddlText = string.Empty;
                    LoadingHelper.ShowLoading($"获取序列{tableName}创建sql语句",() =>
                    {
                        ddlText = oracleDbBll.GetTypeDDLText("SEQUENCE",tableName,owner);
                    });
                    dataGridViewEventArgs.Cell.Value = ddlText;
                }

                if(dataGridViewEventArgs.Column.Name == "REFCOUNT" && string.IsNullOrEmpty(dataGridViewEventArgs.Cell.Value.ToString()))
                {
                    string objectName = dataGridViewEventArgs.Row.Cells["SEQUENCE_NAME"].Value.ToString();//取得对象名
                    string owner = dataGridViewEventArgs.Row.Cells["OWNER"].Value.ToString();//取得表所有者
                    string refCount = string.Empty;
                    LoadingHelper.ShowLoading($"获取{objectName}依赖关系数量",() =>
                    {
                        refCount = oracleDbBll.GetObjectRefCount("SEQUENCE",objectName,owner);
                    });

                    dataGridViewEventArgs.Cell.Value = refCount;
                }
            }
        }


        private void dataDisplayControl_DataReLoad(object sender,EventArgs e)
        {
            InitDate();
        }

        private void 在视图中查找ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            UITabControl parentTabControl = this.GetParentControl<UITabControl>();
            if(parentTabControl != null)
            {
                string name = this.dataDisplayControl.SelectedRowDic["SEQUENCE_NAME"];
                TabControlHelper.AddPageToTabControl(parentTabControl,new OracleViewForm(dbInfo,name),$"{dbInfo.NodeName}-视图",true,$"{name}-OracleViewForm");
            }
        }

        private void 在函数中查找ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            UITabControl parentTabControl = this.GetParentControl<UITabControl>();
            if(parentTabControl != null)
            {
                string name = this.dataDisplayControl.SelectedRowDic["SEQUENCE_NAME"];
                TabControlHelper.AddPageToTabControl(parentTabControl,new OracleFunctionForm(dbInfo,name),$"{dbInfo.NodeName}-函数",true,$"{name}-OracleFunctionForm");
            }
        }

        private void 在存储过程中查找ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            UITabControl parentTabControl = this.GetParentControl<UITabControl>();
            if(parentTabControl != null)
            {
                string name = this.dataDisplayControl.SelectedRowDic["SEQUENCE_NAME"];
                TabControlHelper.AddPageToTabControl(parentTabControl,new OracleProcedureForm(dbInfo,name),$"{dbInfo.NodeName}-存储过程",true,$"{name}-OracleProcedureForm");
            }
        }

        private void 在触发器中查找ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            UITabControl parentTabControl = this.GetParentControl<UITabControl>();
            if(parentTabControl != null)
            {
                string name = this.dataDisplayControl.SelectedRowDic["SEQUENCE_NAME"];
                TabControlHelper.AddPageToTabControl(parentTabControl,new OracleTriggerForm(dbInfo,name),$"{dbInfo.NodeName}-触发器",true,$"{name}-OracleTriggerForm");
            }
        }

        private void 在Jobs中查找ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            UITabControl parentTabControl = this.GetParentControl<UITabControl>();
            if(parentTabControl != null)
            {
                string name = this.dataDisplayControl.SelectedRowDic["SEQUENCE_NAME"];
                TabControlHelper.AddPageToTabControl(parentTabControl,new OracleJobForm(dbInfo,name),$"{dbInfo.NodeName}-Jobs",true,$"{name}-OracleJobForm");
            }
        }

        private void 在包中查找ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            UITabControl parentTabControl = this.GetParentControl<UITabControl>();
            if(parentTabControl != null)
            {
                string name = this.dataDisplayControl.SelectedRowDic["SEQUENCE_NAME"];
                TabControlHelper.AddPageToTabControl(parentTabControl,new OraclePackageForm(dbInfo,name),$"{dbInfo.NodeName}-包",true,$"{name}-OraclePackageForm");
            }
        }

        private void 查看依赖关系ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            UITabControl parentTabControl = this.GetParentControl<UITabControl>();
            if(parentTabControl != null)
            {
                string owner = this.dataDisplayControl.SelectedRowDic["OWNER"];
                string name = this.dataDisplayControl.SelectedRowDic["SEQUENCE_NAME"];
                TabControlHelper.AddPageToTabControl(parentTabControl,new OracleReferenceForm(this.dbInfo,owner,name,"SEQUENCE"),$"{dbInfo.NodeName}-{name}依赖",true,$"{name}-OracleReferenceForm");
            }
        }

        private void 添加到分组ToolStripMenuItem_Click(object sender,EventArgs e)
        {
            string dataBase = this.dataDisplayControl.SelectedRowDic["OWNER"];
            string name = this.dataDisplayControl.SelectedRowDic["SEQUENCE_NAME"];

            DbSourceGroupHelper.AddSourceToGroup(dataBase,name,"TResSequence",this.dbInfo);
        }
    }
}
